#include <iostream>
#include <string>
#include <cstring>
#include "NameManager.h"

using namespace std;

NameManager::NameManager() {
    name = new string [10];
	_size = 0;
	max_size = 10;
}

NameManager::NameManager ( const NameManager &nm) {
    _size = nm.size();
    max_size = nm.get_max_size();
    name = new string [max_size];
	for(int i = 0; i < _size; i++){
		name[i] = nm.get_name()[i];
	}
}

unsigned NameManager::size() const {
    return _size;
}

unsigned NameManager::get_max_size() const {
    return max_size;
}

string* NameManager::get_name() const{
    return name;
}

string NameManager::getName ( int index) const {
    if(index <= _size) return name[index - 1];
}

int NameManager::contains( string name) {
    for (int i = 0; i < _size; i++) {
        if( name.compare(this->name[i]) == 0) return i + 1;
    }
    return -1;
}

void NameManager::check(){
    if (_size >= max_size){
        string *news;
        max_size = max_size + 10;
        news = new string [max_size];
        memcpy(news, name, sizeof(string) * (max_size - 10));
        delete []name;
        name = news;
    }
}

void NameManager::add(string nm) {
    if((*this).contains(nm) == -1) {
        (*this).check();
        name[_size] = nm;
        _size ++;
    }
}

void NameManager::print() {
    for(int i = 0; i < _size; i++){
		cout << name[i] << endl;
	}
}

void NameManager::sort() {
    for ( int i = 0; i < _size - 1;i++) {
        string name_min = name[i];
        int index_name_min = i;
        for ( int j = i + 1; j < _size; j++) {
			int n = name_min.compare(name[j]);
			if( n > 0){
                index_name_min = j;
                name_min = name[j];
            }
		}
        string temp;
        temp = name[i];
        name[i] = name_min;
        name[index_name_min] = temp;
    }
}

void NameManager::del( int index) {
    if( index <= _size){
        string* news;
        news = new string [max_size];
        for ( int t = 0; t < index - 1; t++){
			news[t] = name[t]; 
		}
		int i = index;
        for (i ; i < _size; i++){
            news[i - 1] = name[i];
        }
        delete []name;
        name = news;
		_size--;
    }
}
